package com.example.controller;

import com.example.domain.Visitor;
import com.example.service.VisitorService;
import com.example.utils.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

import static com.example.utils.AjaxResult.*;

@RestController
@RequestMapping("/root")

public class VisitorController {
    @Autowired
    private VisitorService visitorService;

    /** 排队叫号 */
    @RequestMapping("/visitor")
    public AjaxResult visitor(@RequestParam(defaultValue = "1", value = "page") Integer pageNum,
                              @RequestParam(defaultValue = "10", value = "limit") Integer pageSize,
                              @RequestParam(defaultValue = ""  , value = "patient_name") String usr
    ){
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        if (usr == null || usr.equals("")) {
            map.put("usr","");
            List<Visitor> visitors = visitorService.listAll(map);
            PageInfo<Visitor> page = new PageInfo<>(visitors);
            return successData(page.getTotal(), visitors);
        }
        map.put("usr",usr);
        List<Visitor> visitors = visitorService.listAll(map);
        PageInfo<Visitor> page = new PageInfo<>(visitors);
        return successData(page.getTotal(), visitors);
    }
    //挂号新增患者
    @RequestMapping("/addVis")
    public AjaxResult addVis(Visitor visitor){
        int insert = visitorService.insert(visitor);

        int countposition = visitorService.position(visitor.getDoctor_id());
        countposition-=2;
//        int update2 = visitorService.update2(visitor.getDoctor_id(),visitor.getPatient_id(),countposition+1);
        Visitor visitor1=new Visitor(visitor.getDoctor_id(),visitor.getPatient_id(),visitor.getPatient_name(),countposition+1,visitor.getFinish_number(),visitor.getSex(),visitor.getPhone_number(),visitor.getPwd(),visitor.getState());
        int insert1=visitorService.update(visitor1);

        if (1 == insert1){
            return success("添加成功");
        }
        return error("添加失败");

    }

    @RequestMapping("/addVis1")
    public AjaxResult addVis1(Visitor visitor){

        int insert1 = visitorService.insert1(visitor);

//        int coutposition = cout(visitor.getDoctor_id())
//
//        int update2 = visitorService.update2(visitor.getDoctor_id(),visitor.getPatient_id(),coutposition);
        visitorService.update(visitor);
        if (1 == insert1){
            return success("添加成功");
        }
        return error("添加失败");
    }
    //修改患者
    @RequestMapping("/modifyVis")
    public AjaxResult modifyVis(Visitor visitor){
        int update = visitorService.update(visitor);

        if (1 == update){
            return success("修改成功");
        }
        return error("修改失败");
    }
}
